package com.ruoyi.project.tickets.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.tickets.domain.ShopUser;
import com.ruoyi.project.tickets.domain.constant.TicketsConstant;
import com.ruoyi.project.tickets.domain.vo.ShopUserVo;
import com.ruoyi.project.tickets.mapper.ShopUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.tickets.mapper.ShopMapper;
import com.ruoyi.project.tickets.domain.Shop;
import com.ruoyi.project.tickets.service.IShopService;

/**
 * 店铺信息Service业务层处理
 * 
 * @author 孙山伟
 * @date 2023-06-28
 */
@Service
public class ShopServiceImpl implements IShopService 
{
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private ShopUserMapper shopUserMapper;
    /**
     * 查询店铺信息
     * 
     * @param id 店铺信息ID
     * @return 店铺信息
     */
    @Override
    public Shop selectShopById(Long id)
    {
        return shopMapper.selectShopById(id);
    }

    /**
     * 查询店铺信息列表
     * 
     * @param shop 店铺信息
     * @return 店铺信息
     */
    @Override
    public List<Shop> selectShopList(Shop shop)
    {
        List<Shop> shopList=shopMapper.selectShopList(shop);
        for(Shop sh:shopList){
            Long shopId=sh.getId();
            List<ShopUserVo> shopUserVoList= shopUserMapper.selectUserByShopId(shopId);
            List<String> nickNameList =  shopUserVoList.stream().map(ShopUserVo::getNickName).collect(Collectors.toList());
           String name= StringUtils.join(nickNameList,",");
            sh.setUserName(name);
        }
        return shopList;
    }

    /**
     * 新增店铺信息
     * 
     * @param shop 店铺信息
     * @return 结果
     */
    @Override
    public int insertShop(Shop shop)
    {
        shop.setCreateTime(DateUtils.getNowDate());
        return shopMapper.insertShop(shop);
    }

    /**
     * 修改店铺信息
     * 
     * @param shop 店铺信息
     * @return 结果
     */
    @Override
    public int updateShop(Shop shop)
    {
        shop.setUpdateTime(DateUtils.getNowDate());
        return shopMapper.updateShop(shop);
    }

    /**
     * 批量删除店铺信息
     * 
     * @param ids 需要删除的店铺信息ID
     * @return 结果
     */
    @Override
    public int deleteShopByIds(Long[] ids)
    {
        return shopMapper.deleteShopByIds(ids);
    }

    /**
     * 删除店铺信息信息
     * 
     * @param id 店铺信息ID
     * @return 结果
     */
    @Override
    public int deleteShopById(Long id)
    {
        return shopMapper.deleteShopById(id);
    }

    @Override
    public List<ShopUserVo> getUSer(Long id) {
        List<ShopUserVo> shopUserVoList=  shopMapper.selectUSer( TicketsConstant.SHOP_USER);
        ShopUser shopUser=new ShopUser();
        shopUser.setShopId(id);
        List<ShopUser> shopUserList=shopUserMapper.selectShopUserList(shopUser);
        for(ShopUserVo vo:shopUserVoList){
            boolean flag=false;
            for(ShopUser user:shopUserList){
                if(vo.getUserId().equals(user.getUserId())){
                    flag=true;
                    break;
                }
            }
            if(flag){
                vo.setCheck(true);
            }else {
                vo.setCheck(false);
            }
        }
        return shopUserVoList;
    }

    @Override
    public List<Shop> selectListByUserId(Long userId) {
        return shopMapper.selectListByUserId(userId);
    }
}
